Imports System.Drawing
Imports System.Windows.Forms
Imports Vintasoft.Imaging

Namespace Imaging
	Public Partial Class ImageViewerMapSettingsForm
		Inherits Form

		#Region "Fields"

		Private _imageMap As ImageMap

		#End Region



		#Region "Constructros"

		Public Sub New()
			InitializeComponent()

			sizeComboBox.Items.Add("64")
			sizeComboBox.Items.Add("120")
			sizeComboBox.Items.Add("150x200")
			sizeComboBox.Items.Add("200")
			sizeComboBox.Items.Add("320x240")
			sizeComboBox.Items.Add("400x300")
			sizeComboBox.Items.Add("640x480")

			zoomComboBox.Items.Add("1/20")
			zoomComboBox.Items.Add("1/25")
			zoomComboBox.Items.Add("1/40")
			zoomComboBox.Items.Add("1/80")
			zoomComboBox.Items.Add("1/100")
			zoomComboBox.Items.Add("Best fit")

			Dim aStyles As Object() = New Object() {AnchorStyles.None, AnchorStyles.Left Or AnchorStyles.Top, AnchorStyles.Top, AnchorStyles.Top Or AnchorStyles.Right, AnchorStyles.Right, AnchorStyles.Bottom Or AnchorStyles.Right, _
				AnchorStyles.Bottom, AnchorStyles.Bottom Or AnchorStyles.Left, AnchorStyles.Left}

			locationComboBox.Items.AddRange(aStyles)
		End Sub

		Public Sub New(imageMap As ImageMap)
			Me.New()
			_imageMap = imageMap

			ShowSettings()
		End Sub

		#End Region



		#Region "Methods"

		Private Sub ShowSettings()
			enabledCheckBox.Checked = _imageMap.IsEnabled
			alwaysVisibleCheckBox.Checked = _imageMap.IsAlwaysVisible
			locationComboBox.SelectedItem = _imageMap.Anchor
			sizeComboBox.Text = String.Format("{0}x{1}", _imageMap.Size.Width, _imageMap.Size.Height)
			If _imageMap.Zoom = 0 Then
				zoomComboBox.Text = "Best fit"
			Else
				zoomComboBox.Text = String.Format("1/{0}", Math.Round(1 / _imageMap.Zoom))
			End If

			canvasPenCheckBox.Checked = _imageMap.CanvasPen IsNot Nothing
			If canvasPenCheckBox.Checked Then
				canvasPenColorPanel.BackColor = _imageMap.CanvasPen.Color
			End If
			imageBufferPenCheckBox.Checked = _imageMap.ImageBufferPen IsNot Nothing
			If imageBufferPenCheckBox.Checked Then
				imageBufferPenColorPanel.BackColor = _imageMap.ImageBufferPen.Color
			End If
			visibleRectPenCheckBox.Checked = _imageMap.VisibleRectPen IsNot Nothing
			If visibleRectPenCheckBox.Checked Then
				visibleRectPenColorPanel.BackColor = _imageMap.VisibleRectPen.Color
			End If
		End Sub

		Private Function SetSettings() As Boolean
			_imageMap.IsEnabled = enabledCheckBox.Checked
			_imageMap.IsAlwaysVisible = alwaysVisibleCheckBox.Checked
			_imageMap.Anchor = CType(locationComboBox.SelectedItem, AnchorStyles)

			Try
				' Size
				Dim sizeStrings As String() = sizeComboBox.Text.Split("x"C)
				Dim width As Integer
				Dim height As Integer
				If sizeStrings.Length = 1 Then
					width = Convert.ToInt32(sizeStrings(0))
					height = width
				Else
					width = Convert.ToInt32(sizeStrings(0))
					height = Convert.ToInt32(sizeStrings(1))
				End If
				_imageMap.Size = New Size(width, height)

				' Zoom
				If zoomComboBox.Text = "Best fit" Then
					_imageMap.Zoom = 0
				Else
					Dim zoomStrings As String() = zoomComboBox.Text.Split("/"C)
					If zoomStrings.Length <> 2 Then
						Throw New Exception("Invalid zoom value.")
					End If
					_imageMap.Zoom = 1F / Convert.ToInt32(zoomStrings(1))
				End If

				' Pens
				If canvasPenCheckBox.Checked Then
					_imageMap.CanvasPen = New Pen(canvasPenColorPanel.BackColor)
				Else
					_imageMap.CanvasPen = Nothing
				End If

				If visibleRectPenCheckBox.Checked Then
					_imageMap.VisibleRectPen = New Pen(visibleRectPenColorPanel.BackColor)
				Else
					_imageMap.VisibleRectPen = Nothing
				End If

				If imageBufferPenCheckBox.Checked Then
					_imageMap.ImageBufferPen = New Pen(imageBufferPenColorPanel.BackColor)
				Else
					_imageMap.ImageBufferPen = Nothing
				End If
			Catch e As Exception
				DemosTools.ShowErrorMessage(e)
				Return False
			End Try
			Return True
		End Function

		Private Sub buttonOk_Click(sender As Object, e As EventArgs)
			If SetSettings() Then
				DialogResult = DialogResult.OK
			End If
		End Sub

		Private Sub buttonCancel_Click(sender As Object, e As EventArgs)
			DialogResult = DialogResult.Cancel
		End Sub

		Private Sub enabledCheckBox_CheckedChanged(sender As Object, e As EventArgs)
			Dim enabled As Boolean = enabledCheckBox.Checked
			alwaysVisibleCheckBox.Enabled = enabled
			locationComboBox.Enabled = enabled
			sizeComboBox.Enabled = enabled
			zoomComboBox.Enabled = enabled
			canvasPenCheckBox.Enabled = enabled
			imageBufferPenCheckBox.Enabled = enabled
			visibleRectPenCheckBox.Enabled = enabled
		End Sub

		Private Sub canvasPenColorButton_Click(sender As Object, e As EventArgs)
			colorDialog1.Color = canvasPenColorPanel.BackColor
			If colorDialog1.ShowDialog() = DialogResult.OK Then
				canvasPenColorPanel.BackColor = colorDialog1.Color
			End If
		End Sub

		Private Sub visibleRectPenColorButton_Click(sender As Object, e As EventArgs)
			colorDialog1.Color = visibleRectPenColorPanel.BackColor
			If colorDialog1.ShowDialog() = DialogResult.OK Then
				visibleRectPenColorPanel.BackColor = colorDialog1.Color
			End If
		End Sub

		Private Sub imageBufferPenButton_Click(sender As Object, e As EventArgs)
			colorDialog1.Color = imageBufferPenColorPanel.BackColor
			If colorDialog1.ShowDialog() = DialogResult.OK Then
				imageBufferPenColorPanel.BackColor = colorDialog1.Color
			End If
		End Sub

		Private Sub canvasPenCheckBox_CheckedChanged(sender As Object, e As EventArgs)
			canvasPenColorButton.Enabled = canvasPenCheckBox.Checked
			canvasPenColorPanel.Enabled = canvasPenCheckBox.Checked
		End Sub

		Private Sub visibleRectPenCheckBox_CheckedChanged(sender As Object, e As EventArgs)
			visibleRectPenColorButton.Enabled = visibleRectPenCheckBox.Checked
			visibleRectPenColorPanel.Enabled = visibleRectPenCheckBox.Checked
		End Sub

		Private Sub imageBufferPenCheckBox_CheckedChanged(sender As Object, e As EventArgs)
			imageBufferPenColorButton.Enabled = imageBufferPenCheckBox.Checked
			imageBufferPenColorPanel.Enabled = imageBufferPenCheckBox.Checked
		End Sub

		#End Region

	End Class
End Namespace
